package com.designschool.base.http;

import com.designschool.base.notice.HttpRequestListener;
import com.designschool.base.util.MobileUtils;
import com.designschool.base.util.StringUtils;

import android.content.Context;
import android.util.Log;

public class UploadServerRunnable implements Runnable {

	private Context context;
	private String subject;
	private Throwable e;
	private HttpClient httpClient;

	/***
	 * url请求地址，requestname请求接口
	 */
	private static String uploadurl;
	private static String uploadrequestname;

	private HttpRequestParams params;

	public UploadServerRunnable(Context context, Throwable e) {
		this.context = context;
		this.e = e;
		httpClient = new HttpClient();
	}

	/***
	 * 设置接口数据
	 * 
	 * @param url
	 * @param requestname
	 */
	public static void setRequest(String url, String requestname) {
		uploadurl = url;
		uploadrequestname = requestname;
	}

	/***
	 * 设置标题
	 * 
	 * @param subject
	 */
	public void setSubject(String subject) {
		this.subject = subject;
	}

	@Override
	public void run() {
		try {
			StringBuffer sb = new StringBuffer();
			sb.append("手机类型:" + MobileUtils.getMobel() + "  系统版本:"
					+ MobileUtils.getRelease() + "\n");
			StackTraceElement[] ste = e.getStackTrace();
			for (StackTraceElement e : ste) {
				sb.append(e.toString() + "\n");
			}
			params = new HttpRequestParams();
			params.put("subject", subject);
			params.put("content", sb.toString());
			if (!StringUtils.isEmpty(uploadurl) && !StringUtils.isEmpty(uploadrequestname)) {
				httpClient.get(uploadurl, uploadrequestname, params,
						new HttpRequestListener() {
							@Override
							public void OnSuccess(String content,
									String requestname) {
								Log.e(requestname, content);
							}

							@Override
							public void OnStart() {
							}

							@Override
							public void OnFinish() {
							}

							@Override
							public void OnError(String error) {
							}
						}, null, false);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
